/** 
    ____                            
   / __ )_________ __   _____  _____
  / __  / ___/ __ `/ | / / _ \/ ___/
 / /_/ / /  / /_/ /| |/ /  __/ /    
/_____/_/   \__,_/ |___/\___/_/     
* @Title: package-info.java 
* @Package soft.ay.qram.async
* @Description: 进程工厂
* @author vsuns 
* @date 2019年6月6日 
* @version V1.0 
*/
package soft.ay.qram.helpers;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 */
public class CacheThreadFactory implements ThreadFactory
{

    private static final String PREFIX = "QC-";

    /**
     * Creates a new daemon thread factory.
     *
     * @param threadFactoryName
     *            The thread factory name.
     * @return a new daemon thread factory.
     */
    public static CacheThreadFactory createDaemonThreadFactory(final String threadFactoryName) {
        return new CacheThreadFactory(threadFactoryName, true, Thread.NORM_PRIORITY);
    }

    /**
     * Creates a new thread factory.
     *
     * This is mainly used for tests. Production code should be very careful
     * with creating non-daemon threads since those will block application
     * shutdown <br>
     * 生产代码在创建时应该非常小心使用非守护进程线程，因为这些线程将导致应用程序关闭
     *
     * 
     * 
     * @param threadFactoryName
     *            The thread factory name.
     * @return a new daemon thread factory.
     */
    @Deprecated
    public static CacheThreadFactory createThreadFactory(final String threadFactoryName) {
        return new CacheThreadFactory(threadFactoryName, false, Thread.NORM_PRIORITY);
    }

    private static final AtomicInteger FACTORY_NUMBER = new AtomicInteger(1);
    private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
    private final boolean daemon;
    private final ThreadGroup group;
    private final int priority;
    private final String threadNamePrefix;

    /**
     * Constructs an initialized thread factory.
     *
     * @param threadFactoryName
     *            The thread factory name.
     * @param daemon
     *            Whether to create daemon threads.
     * @param priority
     *            The thread priority.
     */
    public CacheThreadFactory(final String threadFactoryName, final boolean daemon, final int priority) {
        this.threadNamePrefix = PREFIX + FACTORY_NUMBER.getAndIncrement() + "-" + threadFactoryName + "-";
        this.daemon = daemon;
        this.priority = priority;
        final SecurityManager securityManager = System.getSecurityManager();
        this.group = securityManager != null ? securityManager.getThreadGroup()
                : Thread.currentThread().getThreadGroup();
    }

    @Override
    public Thread newThread(final Runnable runnable) {
        final Thread thread = new Thread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0);
        if (thread.isDaemon() != daemon) {
            thread.setDaemon(daemon);
        }
        if (thread.getPriority() != priority) {
            thread.setPriority(priority);
        }
        return thread;
    }

}
